//
//  RNOLMyFriendViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/19.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLMyFriendViewController.h"
#import "RNOLMyFriendHeaderView.h"
#import "RNOLMyReferrerViewController.h"
#import "RNOLCommissionRecordListViewController.h"
#import "RNOLFriendsManagerViewController.h"
#import "WGBShareSDKTool.h"

@interface RNOLMyFriendViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,strong) RNOLMyFriendHeaderView *header ;
@end

@implementation RNOLMyFriendViewController

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , KHIGHT) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        adjustsScrollViewInsets_NO(_tableView, self);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 60;
        UIView *headerBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , 250)];
        headerBg.backgroundColor = KWhiteColor;
        RNOLMyFriendHeaderView *header = [RNOLMyFriendHeaderView FromXIB];
        header.frame = headerBg.bounds;
        [headerBg addSubview: header];
        self.header = header;
        
        _tableView.tableHeaderView = headerBg;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview: _tableView];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = kBgColor;
    [self setupNavBarTitleViewWithText:@"我的好友"];
    [self setupNavBarRightButtonWithTitle:@"攻略"];
    [self setup];
    [self actionEvents];
}

#pragma mark- 处理事件
- (void)actionEvents{
    //index =0 佣金明细 1 好友管理
    @weakify(self);
    [self.header setIndexBlock:^(NSInteger index) {
        @strongify(self);
        if (index == 0 ) {
            [self pushPageWithClass:[RNOLCommissionRecordListViewController class]];
        }else{
            [self pushPageWithClass:[RNOLFriendsManagerViewController class]];
        }
    }];
}


#pragma mark- 导航栏右侧 - 攻略
- (void)navRightAction:(UIButton *)button{
    [self showStatusTipWithText: @"攻略"];
}


- (void)setup{
    [RNOLLoginTool setNavgationBarNeedAnimation:YES];
    self.dataSource = @[@"我的推荐人",@"当面邀友",@"分享邀友"];
    [self.tableView reloadData];
}

#pragma mark -  UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = self.dataSource[indexPath.row];
    cell.textLabel.textColor = kTextColor;
    cell.textLabel.font = KFontSize(15);
    return cell;
}

- (void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = self.dataSource[indexPath.row];
    if (kCompareTwoString(title, @"我的推荐人")) {
        [self pushPageWithClass:[RNOLMyReferrerViewController class]];
    }else if (kCompareTwoString(title, @"当面邀友")) {
        
    }else if (kCompareTwoString(title, @"分享邀友")) {
        WGBShareSDKTool *shareTool = [WGBShareSDKTool shareInstance];
        shareTool.title = @"百度一下";
        shareTool.img = [UIImage imageNamed:@"rnol_home_icon004"];
        shareTool.link = @"http://www.baidu.com";
        shareTool.desc = @"分享分享分享分享测试测试分享分享测试测试分享分享测试测试分享分享测试测试分享分享测试测试分享分享测试测试测试测试";
        [shareTool showShareActionSheet];
        [shareTool setQrCodeBlock:^{
            //二维码
            NSLog(@"二维码");
        }];
        [shareTool setReferralCodeBlock:^{
            //推荐码
            NSLog(@"推荐码");
        }];

    }
}

@end
